package io.rong.callkit.huke;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @创建者 喻亚龙
 * @描述
 */

public class BaseApiUrl {

    //K与V之间的拼接字符串 = 或者其他...
    private static final String eqaulsType = "=";

    //K-V与K-V之间的拼接字符串  & 或者|...
    private static final String spliceType = "&";


    public static String getSignKey(Map<String, String> paraMap, String randomNum) {
        String sign = "";
        StringBuilder signKey = new StringBuilder();
        Map<String, String> tmpMap = paraMap;
        try {
            List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(tmpMap.entrySet());
            // 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）
            Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
                @Override
                public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                    return (o1.getKey()).toString().compareTo(o2.getKey());
                }
            });
            // 构造URL 键值对的格式
            for (Map.Entry<String, String> item : infoIds) {
                if (!TextUtils.isEmpty(item.getValue())) {
                    if (signKey.length() == 0) {
                        signKey.append(item.getKey()).append(eqaulsType).append(item.getValue());
                    } else {
                        signKey.append(spliceType).append(item.getKey()).append(eqaulsType)
                                .append(item.getValue());
                    }
                }
            }
//            Log.e("NetBusiness", "signKey:" + signKey.toString());
        } catch (Exception e) {
            return null;
        }
//        token=4vkywbJbak3jVvNAshbnNmOZeHNjds6u7Lc04dImSOVdFYPd475CKwG4pbpFzD9Gf2TSSkaQA2LpxV8Uni8UEQ==&requestId=&signKey=aaaaa!@#$%^&*
        if (!TextUtils.isEmpty(signKey.toString())) {
            signKey.append(spliceType).append("requestId").append(eqaulsType)
                    .append(randomNum);
            signKey.append(spliceType).append("signKey").append(eqaulsType)
                    .append("aaaaa!@#$%^&*");
        }
        sign = MD5Util.stringToMD5(signKey.toString());
        return sign;
    }

}
